<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <%include("/common/header.html",{title:''}){}%>
    <title>控制设备</title>
    <style>
        .layui-form-item {
            margin-bottom: 26px;
            clear: both;
        }
        .layui-form-label {
            float: left;
            display: block;
            padding: 11px 12px;
            width: 84px;
            font-weight: 400;
            line-height: 20px;
            text-align: right;
        }
        .layui-col-md6 {
            width: 38%;
        }

        .element.style {
            margin-left: 300px;
            margin-top: 20px;
        }
    </style>
</head>
<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=IENn7ABARjQIkTeAKyh80VTkg2jYXf3K"></script>
<body>


<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:">首页</a><span lay-separator="">/</span>
        <a><cite>控制设备</cite></a>
      </span>
    <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh-3"></i>
    </a>
</div>


<div class="x-body" style="margin: 20px auto auto 15px;">

    <div style="margin-left: -15px;" class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" name="areaName" placeholder="请输入项目名称" autocomplete="off" class="layui-input" >
            </div>
            <!--<div class="layui-input-inline">-->
            <!--<input type="text" name="e_number" placeholder="请输入设备编号" autocomplete="off" class="layui-input" >-->
            <!--</div>-->
            <div class="layui-input-inline">
                <button  class="layui-btn layui-btn-checked" lay-submit="" lay-filter="LAY-user-front-search"  ><i class="layui-icon layui-icon-search"></i>查询</button>
            </div>

        </div>

    </div>

    <div class="layui-table-main layui-form" style="height: 100%">
        <table id="myTableData" lay-filter="myTableData"  class="layui-hide"></table>
        <div id="test1"></div>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
        <script type="text/html" id="toolbarDemo">
            <a class="layui-btn layui-btn layui-btn-sm"  lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加控制单元</a>
        </script>
        <script type="text/html" id="table-useradmin-webuser">
            <!--<a class="layui-btn layui-bg-green layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>-->
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
    </div>
</div>


<!-- 弹出页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
    <form action class="layui-form" lay-filter="component-form-element" id="myform">
        <input type="hidden" name="id" id="id">
        <input type="hidden"  value="${session.om_user.enterprise.id}" id="" name="">

        <div class="layui-form-item">
            <div class="layui-col-md6" style="float: left">
                <label class="layui-form-label">所属项目：</label>
                <div class="layui-input-block">
                    <select name="departid" lay-filter="departid" id="departid">
                        <option value=""></option>
                    </select>
                </div>
            </div>

            <div class="layui-col-md6" style="float: left">
                <label class="layui-form-label">所属区域：</label>
                <div class="layui-input-block">
                    <select name="areaid"  lay-filter="areaid" id="areaid">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>

        </div>
        <div class="layui-form-item">
            <div class="layui-col-md6" style="float: left">
                <label class="layui-form-label">控制器变量：</label>
                <div class="layui-input-block">
                    <select name="controlVariable" lay-verify=""  id="controlVariable">
                        <option value=""></option>
                    </select>
                </div>
            </div>

            <div class="layui-col-md6" style="float: left">
                <label class="layui-form-label">关联变量：</label>
                <div class="layui-input-block">
                    <select name="variable" lay-verify=""  id="variable">
                        <option value="">请选择</option>
                        <option value=""></option>
                    </select>
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-col-md6" style="float: left">
                <label class="layui-form-label">阈值：</label>
                <div class="layui-input-block">
                    <input type="text" name="threshold" id="threshold" lay-verify="required" autocomplete="off" placeholder="请输入阈值..." class="layui-input">
                </div>
            </div>
            <div class="layui-col-md6" style="float: left">
                <label class="layui-form-label">控制名称：</label>
                <div class="layui-input-block">
                    <input type="text" name="controlName" id="controlName" lay-verify="required" autocomplete="off" placeholder="请输入控制名称..." class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-col-md6" style="float: left">
                <label class="layui-form-label">第一段时间：</label>
                <div class="layui-input-block">
                    <input type="text" name="timestart" id="timestart" lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>

            <div class="layui-col-md6" style="float: left">
                <label class="layui-form-label"style="width: 50px">到</label>
                <div class="layui-input-block">
                    <input type="text" name="timeend" id="timeend" lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-col-md6" style="float: left">
                <label class="layui-form-label">第二段时间：</label>
                <div class="layui-input-block">
                    <input type="text" name="onetimestart" id="onetimestart" lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>

            <div class="layui-col-md6" style="float: left">
                <label class="layui-form-label"style="width: 50px">到</label>
                <div class="layui-input-block">
                    <input type="text" name="onetimeend" id="onetimeend" lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-col-md6" style="float: left">
                <label class="layui-form-label">第三段时间：</label>
                <div class="layui-input-block">
                    <input type="text" name="totimestart" id="totimestart" lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>

            <div class="layui-col-md6" style="float: left">
                <label class="layui-form-label"style="width: 50px">到</label>
                <div class="layui-input-block">
                    <input type="text" name="totimeend" id="totimeend" lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left: 300px;margin-top: 80px;">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element">立即提交</button>
            </div>
        </div>
    </form>
</div>
<script id="demo" type="text/html">
    <option value="">请选择</option>
    {{#  layui.each(d.list, function(index, item){ }}

    <option {{d.depart== item.id? 'selected':""}} value="{{ item.id }}">{{ item.projectName }}</option>

    {{#  }); }}
    {{#  if(d.list.length === 0){ }}
    <option disabled value="">无</option>
    {{#  } }}
</script>
<script id="demo2" type="text/html">
    <option value="">请选择</option>
    {{#  layui.each(d.list2, function(index, item2){ }}

    <option {{d.area== item2.id?"selected":""}} value="{{ item2.id }}">{{ item2.projectName }}</option>

    {{#  }); }}
    {{#  if(d.list2.length === 0){ }}
    <option disabled value="">无</option>
    {{#  } }}
</script>

<script id="demo3" type="text/html">
    <option value="">请选择</option>
    {{#  layui.each(d.list2, function(index, item2){ }}

    <option {{d.valveName== item2.id?"selected":""}} value="{{ item2.id }}">{{ item2.valveName }}</option>

    {{#  }); }}
    {{#  if(d.list2.length === 0){ }}
    <option disabled value="">无</option>
    {{#  } }}
</script>

<script id="demo4" type="text/html">
    <option value="">请选择</option>
    {{#  layui.each(d.list2, function(index, item2){ }}

    <option {{d.dataName== item2.dataName?"selected":""}} value="{{ item2.dataName }}">{{ item2.dataName }}</option>

    {{#  }); }}
    {{#  if(d.list2.length === 0){ }}
    <option disabled value="">无</option>
    {{#  } }}
</script>
<%include("/common/foot.html"){}%>
<script>

    layui.use(['index', 'table', 'form', 'laydate', 'layedit', 'AjaxCommon', 'AliangLayui'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var AjaxCommon = layui.AjaxCommon;
        var AliangLayui = layui.AliangLayui;

        var title = "添加阀门信息";
        var findAll_url = 'system/control/findAll.json';
        var update_url = 'system/control/update.json';
        var delete_url = 'system/control/delete.json';
        var cols = [ //设备信息列表表头
            /*{checkbox: true, fixed: true},*/

            {field: 'zizeng', fixed: 'left',  title: '序号',type:'numbers'},
            {templet: "<div>{{d.area.projectName}}</div>", title: '所属项目'},
            {field: 'controlName',   title: '控制名称'},
            {field: 'controlVariable',   title: '控制器变量'},
            {field: 'variable',   title: '关联变量'},
            {field: 'threshold',   title: '阀值'},

            {title: '操作', fixed: 'right', toolbar: '#table-useradmin-webuser',}
        ];
        //设备信息列表
        AliangLayui.createTable(
            "#LAY-user-manage",
            "listReload",
            "#toolbarDemo",
            title,
            findAll_url,
            true,
            [10, 15, 20, 25],
            ['filter', 'print', 'exports'],
            cols,
            {e_id:$("#e_id").val()}
        );

        //初始化设备信息列表修改页面
        // function loadData(data) {
        //     var ret = {
        //         "id": data.id,
        //         "e_name": data.e_name,
        //         "e_number": data.e_number,
        //         "e_describe": data.e_describe,
        //     };
        //     return ret;
        // }

        //初始化日期模块
        laydate.render({
            elem: '#timestart'//指定元素
            ,type: 'time'
        });
        laydate.render({
            elem: '#timeend'//指定元素
            ,type: 'time'
        });
        laydate.render({
            elem: '#onetimestart'//指定元素
            ,type: 'time'
        });
        laydate.render({
            elem: '#onetimeend'//指定元素
            ,type: 'time'
        });
        laydate.render({
            elem: '#totimestart'//指定元素
            ,type: 'time'
        });
        laydate.render({
            elem: '#totimeend'//指定元素
            ,type: 'time'
        });



        //弹出修改页面调用初始化数据方法
        function initUpdate(data) {
            var a = layer.open({
                type: 1,
                title: title,
                content: $('#layuiadmin-form-useradmin'),
                btn: [], //可以无限个按钮
                area:["100%","100%"],
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
                success: function (layero, index) {
                    layer.iframeAuto(index);
                }
            });
            // layer.full(a);//layer全屏
            //如果是添加则重置表单，如果是编辑则给表单赋值
            if (data == 0 || data == null) {
                $('#myform')[0].reset();
                //项目
                $.ajax({
                    url: "greedLandAdministrator/getDepart.json",
                    type: "POST",
                    data: {},
                    dataType: "json",

                    success: function (ret) {
                        alert(JSON.stringify(1));
                        var laytpl = layui.laytpl;
                        var rets = { //数据
                            "list": ret,
                            depart: data.departid
                        };
                        laytpl($("#demo").html()).render(rets, function (html) {
                            $("#departid").html(html);
                        });
                        form.render();
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                    }
                });
                //区域
                form.on('select(departid)', function (data1) {
                    var departid = data1.value;
                    $.ajax({
                        url: "greedLandAdministrator/getArea.json",
                        type: "POST",
                        data: {
                            departid: departid
                        },
                        dataType: "json",
                        success: function (ret) {
                            alert(JSON.stringify(2));
                            var laytpl = layui.laytpl;
                            var ret2 = { //数据
                                "list2": ret,
                                area: data.areaid
                            };
                            laytpl($("#demo2").html()).render(ret2, function (html) {
                                $("#areaid").html(html);
                            });
                            form.render();
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        }
                    });
                });
                //关联变量
                form.on('select(areaid)', function (data2) {
                    alert(8);
                    alert(JSON.stringify(data2));
                    var areaid = data2.value;
                    $.ajax({
                        url: "system/control/getData.json",
                        type: "POST",
                        data: {
                            areaid: areaid
                        },
                        dataType: "json",
                        success: function (ret) {
                            alert(JSON.stringify(3));
                            var laytpl = layui.laytpl;
                            var ret2 = { //数据
                                "list2": ret,
                                //area: data.areaid
                            };
                            laytpl($("#demo4").html()).render(ret2, function (html) {
                                $("#variable").html(html);
                            });
                            form.render();
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        }
                    });
                });

            } else {
                //项目
                $.ajax({
                    url: "greedLandAdministrator/getDepart.json",
                    type: "POST",
                    data: {},
                    dataType: "json",
                    success: function (ret) {
                        var laytpl = layui.laytpl;
                        var rets = { //数据
                            "list": ret,
                            depart: data.departid
                        };
                        laytpl($("#demo").html()).render(rets, function (html) {
                            $("#departid").html(html);
                        });
                        form.render();
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                    }
                });

                $.ajax({
                    url: "greedLandAdministrator/getArea.json",
                    type: "POST",
                    data: {
                        departid: data.departid
                    },
                    dataType: "json",
                    success: function (ret) {
                        var laytpl = layui.laytpl;
                        var ret2 = { //数据
                            "list2": ret,
                            area: data.areaid
                        };
                        laytpl($("#demo2").html()).render(ret2, function (html) {
                            $("#areaid").html(html);
                        });
                        form.render();
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                    }
                });
                form.on('select(departid)', function (data1) {
                    var departid = data1.value;
                    $.ajax({
                        url: "greedLandAdministrator/getArea.json",
                        type: "POST",
                        data: {
                            departid: departid
                        },
                        dataType: "json",
                        success: function (ret) {
                            var laytpl = layui.laytpl;
                            var ret2 = { //数据
                                "list2": ret,
                                area: data.areaid
                            };
                            laytpl($("#demo2").html()).render(ret2, function (html) {
                                $("#areaid").html(html);
                            });
                            form.render();
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        }
                    });
                });
                layui.form.val("component-form-element", loadData(data));
            }
        }

        //查询控制器变量（阀门名称）
        $.ajax({
            url: "system/control/findValve.json",
            type: "POST",
            data: {
            },
            dataType: "json",
            success: function (ret) {
                var laytpl = layui.laytpl;
                var ret2 = { //数据
                    "list2": ret.data
                };
                laytpl($("#demo3").html()).render(ret2, function (html) {
                    $("#controlVariable").html(html);
                });
                form.render();
            },
            error: function (jqXHR, textStatus, errorThrown) {
            }
        });

        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data) {
            var field = data.field;
            //执行重载
            table.reload('listReload', {
                where :{
                    areaName:field.areaName
                },
                page: {
                    curr: 1//重新从第 1 页开始
                }
            });
        });

        //修改页面的内容提交到后台处理
        form.on('submit(component-form-element)', function (data) {
            postUpdate(data);
            return false;
        });
        //头工具栏事件
        table.on('toolbar(LAY-user-manage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    initUpdate(0);
                    break;
                case 'delete':
                    var data = checkStatus.data;
                    if (data.length > 0) {
                        //var id = new Array(); //声明数组
                        for (var i in data) {
                            var a = data[i].id; //得到复选框的值
                            postDelete(a);
                        }
                    } else {
                        layer.msg("至少选择一个进行删除");
                    }
                    break;
                case 'export':
                    layer.msg("导出");
                    break;
            }
        });

        //点击每行右工具栏“操作”里面按钮效果
        table.on('tool(LAY-user-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('删除以后数据将不能恢复，确定删除吗？', function(index) {
                    postDelete(data.id);
                });
            }else if (obj.event === 'edit') {
                initUpdate(data);
            }
        });


        //ajax提交删除内容
        window.postDelete = function (id) {
            AjaxCommon.ajax(delete_url, "post", "json", {"id": id}, function (ret) {
                if (ret == null) {
                    layer.msg("删除失败", {
                        icon: 5
                    });
                } else {
                    layer.msg(ret.msg, {time: 600, icon: 6}, function () {
                        location.replace(location.href);
                    });
                    layer.closeAll();
                    // table.reload('listReload');
                }
                return ret;
            });
        };

        //ajax提交修改内容
        window.postUpdate = function (data) {
            var formData = new FormData();
            formData.append("data", JSON.stringify(data.field));
            $.ajax({
                url: update_url,
                type: "POST",
                //traditional:true,
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            location.replace(location.href);
                        });
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content: "base/error.jhtml?info=" + estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }
    });

</script>

</body>

</html>
